package com.sugar.subscribe.course.module.business.service.coachvideo;

import cn.hutool.core.collection.CollUtil;
import com.sugar.subscribe.course.module.business.controller.app.coachvideo.vo.AppCoachVideoRespVO;
import com.sugar.subscribe.course.module.business.controller.app.coachvideo.vo.AppCoachVideoSaveReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.sugar.subscribe.course.module.business.controller.admin.coachvideo.vo.*;
import com.sugar.subscribe.course.module.business.dal.dataobject.coachvideo.CoachVideoDO;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.common.pojo.PageParam;
import com.sugar.subscribe.course.framework.common.util.object.BeanUtils;

import com.sugar.subscribe.course.module.business.dal.mysql.coachvideo.CoachVideoMapper;

import static com.sugar.subscribe.course.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.subscribe.course.framework.common.util.collection.CollectionUtils.convertList;
import static com.sugar.subscribe.course.framework.common.util.collection.CollectionUtils.diffList;
import static com.sugar.subscribe.course.module.business.enums.ErrorCodeConstants.*;

/**
 * 教练视频介绍 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class CoachVideoServiceImpl implements CoachVideoService {

    @Resource
    private CoachVideoMapper coachVideoMapper;

    @Override
    public Long createCoachVideo(CoachVideoSaveReqVO createReqVO) {
        // 插入
        CoachVideoDO coachVideo = BeanUtils.toBean(createReqVO, CoachVideoDO.class);
        coachVideoMapper.insert(coachVideo);

        // 返回
        return coachVideo.getId();
    }

    @Override
    public void updateCoachVideo(CoachVideoSaveReqVO updateReqVO) {
        // 校验存在
        validateCoachVideoExists(updateReqVO.getId());
        // 更新
        CoachVideoDO updateObj = BeanUtils.toBean(updateReqVO, CoachVideoDO.class);

        if (updateReqVO.getAuditStatus().equals(10))
            updateObj.setRefuseCause("");

        coachVideoMapper.updateById(updateObj);
    }

    @Override
    public void deleteCoachVideo(Long id) {
        // 校验存在
        validateCoachVideoExists(id);
        // 删除
        coachVideoMapper.deleteById(id);
    }

    @Override
        public void deleteCoachVideoListByIds(List<Long> ids) {
        // 删除
        coachVideoMapper.deleteByIds(ids);
        }


    private void validateCoachVideoExists(Long id) {
        if (coachVideoMapper.selectById(id) == null) {
            throw exception(COACH_VIDEO_NOT_EXISTS);
        }
    }

    @Override
    public CoachVideoDO getCoachVideo(Long id) {
        return coachVideoMapper.selectById(id);
    }

    @Override
    public PageResult<CoachVideoRespVO> getCoachVideoPage(CoachVideoPageReqVO pageReqVO) {
        return coachVideoMapper.selectPage(pageReqVO);
    }

    @Override
    public void appCreateCoachVideo(AppCoachVideoSaveReqVO createReqVO, Long coachId) {
        // 插入
        CoachVideoDO coachVideo = new CoachVideoDO();
        coachVideo.setCoachId(coachId).setVideoUrl(createReqVO.getVideoUrl()).setAuditStatus(0);
        coachVideoMapper.insert(coachVideo);
    }

    @Override
    public PageResult<AppCoachVideoRespVO> appGetCoachVideoPage(PageParam pageParam, Long coachId) {
        return BeanUtils.toBean(coachVideoMapper.selectPage(BeanUtils.toBean(pageParam, CoachVideoPageReqVO.class).setCoachId(coachId)), AppCoachVideoRespVO.class);
    }

}